//
// Created by Administrator on 2022/10/17.
//
//循环链表 使得从一个节点访问到所有节点变成可能
//为了更加方便的访问头节点，可以使用尾指针

#include <malloc.h>
#include <stdio.h>

#define OK 1
#define ERROR 0
typedef int ElemType;

typedef struct Node {
    ElemType Data;
    struct Node* next;
}Node;
typedef struct Node* LinkedList;


//新建一个链表,并随机从插入i个数据,采用尾插法 ,并返回尾指针

LinkedList createLinkedList2 (LinkedList * list,int i ){
    *list = (LinkedList)malloc(sizeof(Node));
    LinkedList p = *list;
    LinkedList q;
    p->next = NULL;
    for (int j = 0; j < i; ++j) {
        q = (LinkedList) malloc(sizeof(Node));
        q->Data = j;
        q->next = 0;
        p->next = q;
        p=p->next;
    }
    p->next =*list;
    //尾指针
    return p;
}
//显示循环链表中的每个元素
void showLinkedList (LinkedList list){
    LinkedList p = list->next;
    while(p!=list){
        printf("%d\n",p->Data);
        p=p->next;
    }
}

void main(){
    LinkedList l;
    LinkedList l2;

    LinkedList rear1 = createLinkedList2(&l,9);

    LinkedList rear2 = createLinkedList2(&l2,5);
    printf("1:\n");
    showLinkedList(l);
    printf("2:\n");
    showLinkedList(l2);
    printf("3:\n");
    //将两个循环链表合并成一个循环链表
    LinkedList tmp = rear1->next;
    rear1->next = rear2->next->next;
    LinkedList tmp2 = rear2->next;
    rear2->next=tmp;
    free(tmp2);
    showLinkedList(l);


}

